﻿

using System;
using System.Collections.Generic;
using System.Linq;
using Web.Models;
using Model.Core.DataBase;
using Web.Models.AnalysisData;
using Web.Models.CoreData;
using System.IO;

namespace Web.BML.CoreData
{
    /// <summary>
    /// 终端用户
    /// </summary>
    public class UserOp
    {

        private static UserOp instance;
        /// <summary>
        /// 构造函数
        /// </summary>
        private UserOp()
        {

        }
        /// <summary>
        /// 单例模式实例化
        /// </summary>
        /// <returns></returns>
        public static UserOp GetInstance()
        {
            if (instance == null)
            {
                instance = new UserOp();
            }
            return instance;
        }

        #region 查询

        /// <summary>
        ///    查询用户
        /// </summary>
        /// <param name="token">用户许可号</param>
        /// <returns></returns>
        public User QueryUser(String token)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<User>().FirstOrDefault(m=>m.Account.Equals(token));
            }
            
        }

        /// <summary>
        ///    查询用户
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns></returns>
        public User QueryUserId(String id)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<User>().FirstOrDefault(m => m.Id == id.ToString());
            }
        }

        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        public List<User> QueryAllUser()
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<User>().ToList();
            }
        }

        /// <summary>
        /// 查询使用该配置项的用户
        /// </summary>
        /// <param name="configs">配置项ID</param>
        /// <returns></returns>
        public List<User> QueryUserConfig(String configs)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<User>().Where(m=>m.Configs.Equals(configs)).ToList();
            }
        }


        /// <summary>
        /// 查询公司下所有用户
        /// </summary>
        /// <returns></returns>
        public List<User> QueryCompanyUser(String companyId)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<User>().Where(m=>m.Company.Equals(companyId)).ToList();
            }
        }

        #endregion

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="model"></param>
        public int ModifUser(User model)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                db.GetOperator<User>().Modify(model);
                return db.SaveAllChange();
            }
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="model"></param>
        public int CreateUser(User model)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                db.GetOperator<User>().Create(model);
                return db.SaveAllChange();
            }
        }


    }


}

